<?
require('../includes/class.main.php');

$manager = new MeetingManager();

if (isset($_POST['submit']) && $_POST['submit'] == "Upload") {

	$errorcodes = array(
			UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
			UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
			UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
			UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
			UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
			UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
			UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
	);
	
	$error = $_FILES['xlsdata']['error'];
	$result = "";
	
	$target_path = "../xlsdata/calendar_data.xls";
	$tmp_path = $_FILES['xlsdata']['tmp_name'];
	$file_name = basename( $_FILES['xlsdata']['name']);
	
	if(!move_uploaded_file($tmp_path, $target_path)) {

		$result = "<b>ERROR ($error):</b><br> $errorcodes[$error]";
	
	} else { 
		chdir('../bin');
		$perlcmd = Props::get("exec", "perl");
		exec($perlcmd . ' ./CalendarDataLoad.pl ../xlsdata/calendar_data.xls', $output, $return);
	
		if ($return != 0) {
			
			$result = "<font color=\"red\">ERROR: File Upload Failed </font>\n";
			$result .= "An error was encountered during processing.\n\n";
			
			foreach ($output as $line) {
				$result .= $line."\n";
			}
				
		} else {
			$result .= "The " . $file_name . " file has been loaded successfuly.  \n\n";
		}
				
		chdir('../admin');
	}
	
	$_SESSION['updateStatus'] = $result;
}

header('Location: update-calendar.php', true, 303);

?>
